package com.example.bean;

import com.example.bean.RegisterBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/register") // 设置 Servlet 的 URL 映射
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 1. 从请求中获取数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String confirmPassword = request.getParameter("confirmPassword");

        // 2. 创建 JavaBean 并设置数据
        RegisterBean registerBean = new RegisterBean();
        registerBean.setUsername(username);
        registerBean.setPassword(password);
        registerBean.setConfirmPassword(confirmPassword);
        // 3. 调用 JavaBean 进行注册
        registerBean.register();
        // 4. 获取注册结果
        if (registerBean.getRegisterResult()){
            request.setAttribute("registerBean", registerBean);
            request.getRequestDispatcher("/registerSuccess.jsp").forward(request,response);
        } else{
            request.setAttribute("registerBean", registerBean);
            request.getRequestDispatcher("/register.jsp").forward(request,response);
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/register.jsp").forward(request,response);
    }
}